import 'dart:async';
import 'dart:io';
import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:app/src/config/app_data_config.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/http/bean/http_data.dart';
import 'package:app/src/http/bean/user.dart';
import 'package:app/src/http/bean/ywx_count.dart';
import 'package:app/src/http/bean/ywx_limit.dart';
import 'package:app/src/http/sign.dart';
import 'package:app/src/http/url.dart';
import 'package:app/src/manager/user_manager.dart';
import 'package:app/src/manager/ywx_manager.dart';
import 'package:app/src/ui/dialog/dailog.dart';

///权限相关
class YwxModel {
  ///获取菜单权限
  static Future<List<YwxLimit>?> getYwxLimits(
      BuildContext context, bool isEnableLoad) async {
    Completer completer = new Completer<List<YwxLimit>?>();
    var url = Url.baseurl() + Url.checkgetlimit;
    var token = AppDataConfig.getToken();
    var subData = SubData();
    subData.commondata = CommonData();
    var queryParameters = Map<String, dynamic>();
    queryParameters['data'] = subData.jsonEncodeToJson();
    var sign = getSign(queryParameters);
    queryParameters['sign'] = sign;
    //print('签名：$sign');
    KOhttp(url)
      ..isEnableLoad(isEnableLoad, context)
      // ..onStart(() {
      //   print('开始');
      // })
      ..onError((code, error) {
        // print('错误:\t$code\t$error');
        AppDialog.showDialogWithError(context, code, error);
        completer.complete(null);
      })
      ..onSuccess((result) {
        // print('菜单权限成功:\n$result');
      })
      // ..onFinish(() {
      //   print('结束');
      // })
      ..isCache(false)
      ..addHeader('token', token)
      ..addFormDatas(queryParameters)
      ..contentType = ContentType.form
      ..isEnableLog = AppDataConfig.isEnableLog
      ..postASync<AppHttpResult<List<YwxLimit>, YwxLimit>>(AppHttpResult()
            ..Data = <YwxLimit>[]
            ..t2 = YwxLimit())
          ?.then((result) {
        result?.Data?.forEach((element) {
          ///存储所有的菜单权限
          YwxIdManager.put(element.YwxId, element);
          // var json = element.jsonEncodeToJson();
          // if (json.contains("工单")) {
          //   print('菜单权限:\n${json}');
          //   // {"YwxId":"N7206","YwxName":"工单中心","Group":"1000","HasLimit":"True"}
          //   // {"YwxId":"N7207","YwxName":"工单查询","Group":"1000","HasLimit":"True"}
          //   // {"YwxId":"N7208","YwxName":"我的工单","Group":"1000","HasLimit":"False"}
          // }
        });
        completer.complete(result.Data);
      });
    return completer.future as Future<List<YwxLimit>?>;
  }

  ///获取列表显示条目数字（未审核个数）
  static Future<YwxCount?>? getYwxCount(
      BuildContext context, bool isEnableLoad) async {
    if (UserManager.getUserInfo() == null) {
      //fixme 判断用户是否登陆
      return null;
    }
    Completer completer = new Completer<YwxCount>();
    var url = Url.baseurl() + Url.checkgetnum;
    var token = AppDataConfig.getToken();
    var subData = SubData();
    subData.commondata = CommonData();
    var queryParameters = Map<String, dynamic>();
    queryParameters['data'] = subData.jsonEncodeToJson();
    var sign = getSign(queryParameters);
    queryParameters['sign'] = sign;
    //print('签名：$sign');
    KOhttp(url)
      ..isEnableLoad(isEnableLoad, context)
      // ..onStart(() {
      //   print('开始');
      // })
      ..onError((code, error) {
        // print('错误:\t$code\t$error');
        // if(isEnableLoad){
        //   AppDialog.showDialogWithError(context, code, error);//这里错误就不显示了。
        // }
      })
      // ..onSuccess((result) {
      //   print('主页面条目个数数据返回:\n$result');
      // })
      // ..onFinish(() {
      //   print('结束');
      // })
      ..isCache(false)
      ..addHeader('token', token)
      ..addFormDatas(queryParameters)
      ..isAllowedRepeat = true
      ..contentType = ContentType.form
      ..isEnableLog = AppDataConfig.isEnableLog
      ..postASync<AppHttpResult<YwxCount, String>>(AppHttpResult()
            ..Data = YwxCount()
            ..t2 = "")
          ?.then((result) {
        completer.complete(result.Data);
      });
    return completer.future as Future<YwxCount>;
  }
}
